package homelesspartners.server.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ScriptRunner {

	private static final String SCRIPT_LOCATIONS = "/workspace/homelesspartners/src/homelesspartners/server/utils/";
	private static final String PWD = "spouda22";
	private static final String USER = "root";
	private static final String DELETE_DB = "DeleteDatabase.sql";
	private static final String CREATE_DB = "CreateDatabase.sql";
	private static final String POPULATE_DB = "LoadSampleData.sql";
	private static final String POPULATE_DB_Load = "LoadData.sql";
	private static final String POPULATE_DB_Load_Thanks = "LoadThankYouData.sql";

	private static final String[][] scripts =
			new String[][] { new String[] { CREATE_DB }, new String[] { DELETE_DB }, new String[] { POPULATE_DB },
					new String[] { DELETE_DB, CREATE_DB }, new String[] { CREATE_DB, POPULATE_DB },
					new String[] { DELETE_DB, CREATE_DB, POPULATE_DB }, new String[] { POPULATE_DB_Load },
					new String[] { POPULATE_DB_Load_Thanks },
					new String[] { DELETE_DB, CREATE_DB, POPULATE_DB, POPULATE_DB_Load, POPULATE_DB_Load_Thanks } };

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		for (int x = 0; x < scripts.length; x++) {
			System.out.print("OPTION " + x + ":");
			for (int y = 0; y < scripts[x].length; y++) {
				System.out.print(" " + scripts[x][y]);
			}
			System.out.println("");
		}

		System.out.print("Enter your script number: ");

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int scriptNum = -1;

		try {
			String input = br.readLine();
			scriptNum = Integer.parseInt(input);
		} catch (IOException ioe) {
			System.out.println("IO error trying to read your name!");
			System.exit(1);
		}

		if (scriptNum < 0 || scriptNum > scripts.length) {
			System.err.println("Unable to identify the script to run.");
			return;
		}

		runScriptOption(scriptNum);

	}

	public static void runScriptOption(int scriptNum) {
		for (int x = 0; x < scripts[scriptNum].length; x++) {
			executeScript(null, USER, PWD, SCRIPT_LOCATIONS + scripts[scriptNum][x], true);
		}
	}

	public static String executeScript(String dbname, String dbuser, String dbpassword, String scriptpath,
			boolean verbose) {
		String output = null;
		try {
			String[] cmd = null;
			if (dbname != null) {
				cmd =
						new String[] { "mysql", dbname, "--user=" + dbuser, "--password=" + dbpassword, "-e",
								"\"source " + scriptpath + "\"" };
			} else {
				cmd =
						new String[] { "mysql", "--user=" + dbuser, "--password=" + dbpassword, "-e",
								"\"source " + scriptpath + "\"" };
			}
			for (int x = 0; x < cmd.length; x++) {
				System.out.print(cmd[x] + " ");
			}
			System.out.println("");
			Process proc = Runtime.getRuntime().exec(cmd);
			if (verbose) {
				InputStream inputstream = proc.getInputStream();
				InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
				BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

				// read the output
				String line;
				while ((line = bufferedreader.readLine()) != null) {
					System.out.println(line);
				}

				// check for failure
				try {
					if (proc.waitFor() != 0) {
						System.err.println("exit value = " + proc.exitValue());
					}
				} catch (InterruptedException e) {
					System.err.println(e);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return output;
	}
}
